package cmd

import (
	"github.com/shirou/gopsutil/process"
	"github.com/spf13/cobra"
	"github.com/wonderivan/logger"
	"log"
	"strings"
)

func Stop() *cobra.Command {
	var stopCmd = &cobra.Command{
		Use:     "stop",
		Version: "v0.0.1",
		Short:   UsageGenConf,
		Long:    UsageGenConf,
		Run: func(cmd *cobra.Command, args []string) {
			processes, _ := process.Processes()
			for _, p := range processes {
				if name, _ := p.Exe(); strings.Count(name, "openresty") > 0 {
					err := p.Kill()
					if err != nil {
						log.Fatalf("Stop the process failed!<pid=%d>", p.Pid)
					}
				}
			}
			logger.Info("Kill process nginx suc!")
		},
	}
	return stopCmd
}
